package com.agent;

import com.interceptor.GatewayDelegateLogic;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;

import java.lang.instrument.Instrumentation;

public class T_05_DelegateGatewayAgent {

    public static void premain(String args, Instrumentation inst) {
        install(inst);
    }

    public static void agentmain(String args, Instrumentation inst) {
        install(inst);
    }

    private static void install(Instrumentation inst) {
        new AgentBuilder.Default()
                .type(ElementMatchers.named("com.gillion.gateway.filter.TokenFilter"))
                .transform((builder, td, cl, m) ->
                        builder.method(ElementMatchers.named("shouldFilter"))
                                // 1. 整个方法委派给 MyLogic 里的静态方法
                                .intercept(MethodDelegation.to(GatewayDelegateLogic.class))
                )
                .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
                .installOn(inst);
    }
}